package com.colorgame.data;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.ArrayList;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.graphics.Color;
import android.view.Gravity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.example.color_game.R;

public class ScoreList {
	private Object[] lastAdded = null;
	
	private ArrayList<Object[]> scores = new ArrayList<Object[]>();
	private final static int position = 0;
	private final static int name = 1;
	private final static int score = 2;

	final Activity activity;

	public ScoreList(Activity a) {
		activity = a;
	}

	public int add(String name, int score) {
		Object[] o = new Object[] { 0, name, score };

		scores.add(o);
		
		lastAdded = o;

		return scores.indexOf(o);
	}

	private void remove(int id) {
		scores.remove(id);
	}

	public void clear() {
		scores.clear();
	}

	public int size() {
		return scores.size();
	}

	private Object[] get(int id) {
		return scores.get(id);
	}

	
	public int getPosition(int id) {
		return (Integer) scores.get(id)[position];
	}

	public String getName(int id) {
		return "" + scores.get(id)[name];
	}

	public int getScore(int id) {
		return (Integer) scores.get(id)[score];
	}

	private String getFormatedString(int id) {
		Object[] o = scores.get(id);

		return o[position] + ".\t" + o[name] + "\t\t" + o[score];
	}

	
	public void write(final ListView lv) {
		String[] values = new String[size()];
		for (int i = 0; i < size(); i++) {
			values[i] = getFormatedString(i);
		}

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(
				activity.getBaseContext(), android.R.layout.activity_list_item,
				android.R.id.text1, values);

		lv.setAdapter(adapter);
	}

	public void write(final TableLayout tl) {
		tl.removeAllViews();

		for (int i = 0; i < size(); i++) {
			TableRow row = new TableRow(activity);

			TextView t1 = new TextView(activity);
			TextView t2 = new TextView(activity);
			TextView t3 = new TextView(activity);

			// t1.setText(getPosition(i) + ".");
			t1.setText(i + 1 + ".");
			t2.setText(getName(i));
			t3.setText(getScore(i) + "p");

			if (scores.get(i) == lastAdded) {
				t1.setBackgroundColor(Color.GREEN);
				t2.setBackgroundColor(Color.YELLOW);
				t3.setBackgroundColor(Color.RED);
			}

			t1.setGravity(Gravity.CENTER);
			t2.setGravity(Gravity.CENTER);
			t3.setGravity(Gravity.CENTER);
			

			t1.setWidth(60);
			t2.setWidth(300);
			t3.setWidth(300);
			
			t1.setTextSize(20);
			t2.setTextSize(20);
			t3.setTextSize(20);
			
			row.addView(t1);
			row.addView(t2);
			row.addView(t3);

			tl.addView(row, new TableLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

		}
	}

	public void sort() {
		Object[] temp;

		for (int n = scores.size(); n > 1; n = n - 1) {
			for (int i = 0; i < n - 1; i = i + 1) {
				if (getScore(i) < getScore(i + 1)) {

					temp = scores.get(i);

					scores.set(i, scores.get(i + 1));
					scores.set(i + 1, temp);

				}
			}
		}

	}

	public void save() {
		write(activity.getDir("\\colorgame\\", activity.MODE_PRIVATE) + "meinFile.txt" );
	}

	public void load() {
		final String path = activity.getDir("\\colorgame\\", activity.MODE_PRIVATE) + "meinFile.txt";
		if (new File(path).exists()) {
			read(path);		
		} else {
			write(path);
		}
	}

	private void write(final String filename) {
		try {
			FileOutputStream fs = new FileOutputStream(filename);
			ObjectOutputStream os = new ObjectOutputStream(fs);
			os.writeObject(scores);
			os.close();
		} catch (FileNotFoundException e) { 
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void read(final String filename) {
		try {
			FileInputStream fs = new FileInputStream(filename);
			ObjectInputStream is = new ObjectInputStream(fs);
			clear();
			scores = (ArrayList<Object[]>) is.readObject();
			sort();
		} catch (StreamCorruptedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}	

	
	public void reset() {
		clear();
		save();
	}
	
}
